﻿
Imports System.IO
Imports System.Net.Sockets
Imports osi.root.template
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.procedure

Public Class answer_bond(Of T,
                            CREATE_T As __do(Of T),
                            HANDLER_T,
                            CATCH_HANDLER As __do(Of T, HANDLER_T),
                            ANSWER_HANDLER As __void(Of TcpClient, powerpoint, HANDLER_T))
    Public Shared ReadOnly [default] As answer_bond(Of T, CREATE_T, HANDLER_T, CATCH_HANDLER, ANSWER_HANDLER)
    Private Shared ReadOnly ctor As CREATE_T
    Private Shared ReadOnly catcher As CATCH_HANDLER
    Private Shared ReadOnly answerer As ANSWER_HANDLER

    Shared Sub New()
        ctor = alloc(Of CREATE_T)()
        catcher = alloc(Of CATCH_HANDLER)()
        answerer = alloc(Of ANSWER_HANDLER)()
    End Sub

    Default Public ReadOnly Property ans(ByVal p As powerpoint) As Boolean
        Get
            Return tcp.answer(p, AddressOf answer)
        End Get
    End Property

    Public Sub answer(ByVal c As TcpClient, ByVal p As powerpoint)
        Dim i As T = Nothing
        i = +ctor
        Dim h As HANDLER_T = Nothing
        h = catcher(i)
        answerer.invoke(c, p, h)
    End Sub
End Class

Public Class answer_bytes_bond(Of T,
                                  CREATE_T As __do(Of T),
                                  CATCH_HANDLER As __do(Of T, Func(Of Byte(), pointer(Of Byte()), event_comb)))
    Inherits answer_bond(Of T, 
                            CREATE_T, 
                            Func(Of Byte(), pointer(Of Byte()), event_comb), 
                            CATCH_HANDLER, 
                            answer_handle)

    Public Shared Shadows ReadOnly [default] As answer_bytes_bond(Of T, CREATE_T, CATCH_HANDLER)

    Shared Sub New()
        [default] = New answer_bytes_bond(Of T, CREATE_T, CATCH_HANDLER)()
    End Sub

    Public Class answer_handle
        Inherits __void(Of TcpClient, powerpoint, Func(Of Byte(), pointer(Of Byte()), event_comb))

        Public Overrides Sub at(ByRef i As TcpClient,
                                ByRef j As powerpoint,
                                ByRef k As Func(Of Byte(), pointer(Of Byte()), event_comb))
            tcp.answer(i, j, k)
        End Sub
    End Class
End Class

Public Class answer_stream_bond(Of T,
                                   CREATE_T As __do(Of T),
                                   CATCH_HANDLER As __do(Of T, Func(Of Stream, event_comb)))
    Inherits answer_bond(Of T, 
                            CREATE_T, 
                            Func(Of Stream, event_comb), 
                            CATCH_HANDLER, 
                            answer_handle)

    Public Shared Shadows ReadOnly [default] As answer_stream_bond(Of T, CREATE_T, CATCH_HANDLER)

    Shared Sub New()
        [default] = New answer_stream_bond(Of T, CREATE_T, CATCH_HANDLER)()
    End Sub

    Public Class answer_handle
        Inherits __void(Of TcpClient, powerpoint, Func(Of Stream, event_comb))

        Public Overrides Sub at(ByRef i As TcpClient,
                                ByRef j As powerpoint,
                                ByRef k As Func(Of Stream, event_comb))
            tcp.answer(i, j, k)
        End Sub
    End Class
End Class

Public Class answer_client_bond(Of T,
                                   CREATE_T As __do(Of T),
                                   CATCH_HANDLER As __do(Of T, Func(Of TcpClient, event_comb)))
    Inherits answer_bond(Of T, 
                            CREATE_T, 
                            Func(Of TcpClient, event_comb), 
                            CATCH_HANDLER, 
                            answer_handle)

    Public Shared Shadows ReadOnly [default] As answer_client_bond(Of T, CREATE_T, CATCH_HANDLER)

    Shared Sub New()
        [default] = New answer_client_bond(Of T, CREATE_T, CATCH_HANDLER)()
    End Sub

    Public Class answer_handle
        Inherits __void(Of TcpClient, powerpoint, Func(Of TcpClient, event_comb))

        Public Overrides Sub at(ByRef i As TcpClient,
                                ByRef j As powerpoint,
                                ByRef k As Func(Of TcpClient, event_comb))
            tcp.answer(i, j, k)
        End Sub
    End Class
End Class
